<template>
  <div class="app-container">
    <el-calendar v-model="value" style="width: 1100px">
      <template
        slot="dateCell"
        slot-scope="{ date, data }">
        <div class="calendar-div" @click="handleDialog({...{day: data.day, holiday: list[data.day] ? list[data.day] : ''},...{
              dTitle: '节假日设定',
              dType: 'add',
              dField: dField
            }})">
          <el-row>
            <el-col class="calendar-date pr">
              <span :class="'text ' + (list[data.day] ? 'is-selected' : '')">{{ date.getDate() }}</span>
              <div v-if="list[data.day] && list[data.day].type === 1" class="calendar-rest pa">普休</div>
              <div v-if="list[data.day] && list[data.day].type === 2" class="calendar-rest pa">国假</div>
            </el-col>
            <el-col>
              <div v-if="list[data.day]" class="calendar-remark">{{list[data.day].remark}}</div>
            </el-col>
          </el-row>
        </div>
      </template>
    </el-calendar>
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { queryHolidays } from '@/api/base/holiday'
import Dialog from './components/holiday-dialog'
import page from '@/utils/page'

export default {
  name: '',
  components: { Dialog },
  extends: page,
  props: {
    reqList: {
      default: queryHolidays,
      type: Function
    }
  },
  watch: {
    'value': {
      handler(n, o) {
        let day = this.getDay()
        if(day !== this.filter.day) {
          this.filter.day = day
          this.getList()
        }
      },
      immediate: false
    }
  },
  data() {
    return {
      value: new Date(),
      dField: ['day', 'holiday']
    }
  },
  created() {
    this.filter.day = this.getDay()
    this.getList()
  },
  methods: {
    getDay(){
      let year = this.value.getFullYear()
      let month = this.value.getMonth() + 1

      month < 10 ? month = '0'+month: month

      return year + '-' + month + '-01'
    },
    formatterType(e) {
      if(e.type === 2) return '国假'
      return '普假'
    }
  }
}
</script>

<style>
  .calendar-div{
    padding: 0;
    margin: 0;
    height: 69px;
  }
  .is-selected {
    color: #fa9819;
  }
  .calendar-date{
    height: 30px;
    line-height: 30px;
  }
  .calendar-rest {
    text-align: center;
    color: #fff;
    border-radius: 5px;
    background: rgb(250, 124, 77);
    width: 35px;
    height: 20px;
    line-height: 20px;
    font-size: 12px;
    top: 3px;
    left: 15px;
    margin-left: 10px;
  }
  .calendar-remark{
    padding: 5px 0;
    font-size: 12px;
    color: #333;
    width: 100%;
    height: 39px;
    overflow: hidden;
  }
</style>
